package com.longge;

import java.util.LinkedHashMap;
import java.util.Map;

/**
 * @创建人 xinglongge
 * @创建时间 2021/11/23
 * @描述
 */
public class FirstUniqChar {


    public int firstUniqChar(String s) {

        char[] chars = s.toCharArray();
        LinkedHashMap<Character, Integer> charMap = new LinkedHashMap<>();
        LinkedHashMap<Character, Integer> indexMap = new LinkedHashMap<>();
        for (int i = 0; i < chars.length; i++) {
            charMap.put(chars[i], charMap.getOrDefault(chars[i], 0) + 1);
            indexMap.putIfAbsent(chars[i], i);
        }
        for (Map.Entry<Character, Integer> entry : charMap.entrySet()) {
            if (entry.getValue() == 1) return indexMap.get(entry.getKey());
        }
        return -1;
    }

}
